package com.dcrzl.shcz.module.info.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import com.dcrzl.shcz.common.entity.info.Gov;
import com.dcrzl.shcz.common.entity.info.GovUnit;
import com.dcrzl.shcz.common.excel.ReviewOrderExcel;
import com.dcrzl.shcz.common.mapper.info.ReviewOrderMapper;
import com.dcrzl.shcz.common.service.impl.BaseServiceImpl;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.common.utils.SysConstant;
import com.dcrzl.shcz.common.utils.SysUtils;
import com.dcrzl.shcz.module.info.service.GovService;
import com.dcrzl.shcz.module.info.service.ReviewOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

import com.dcrzl.shcz.common.entity.info.ReviewOrder;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@Slf4j
@Service("reviewOrderService")
public class ReviewOrderServiceImpl extends BaseServiceImpl<ReviewOrderMapper, ReviewOrder> implements ReviewOrderService {

    @Autowired
    private GovService govService;

    private LambdaQueryWrapper<ReviewOrder> commonWrapper(ReviewOrder reviewOrder) {
        LambdaQueryWrapper<ReviewOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(reviewOrder.getId()), ReviewOrder::getId, reviewOrder.getId());
        wrapper.eq(Objects.nonNull(reviewOrder.getStatus()), ReviewOrder::getStatus, reviewOrder.getStatus());
        wrapper.eq(Objects.nonNull(reviewOrder.getOrderStatus()), ReviewOrder::getOrderStatus, reviewOrder.getOrderStatus());
        wrapper.eq(Objects.nonNull(reviewOrder.getGovType()), ReviewOrder::getGovType, reviewOrder.getGovType());
        wrapper.eq(Objects.nonNull(reviewOrder.getGovUnitId()), ReviewOrder::getGovUnitId, reviewOrder.getGovUnitId());
        wrapper.like(StringUtils.hasText(reviewOrder.getItemName()), ReviewOrder::getItemName, reviewOrder.getItemName());
        if (StringUtils.hasText(reviewOrder.getGovUnitIdListStr())) {
            wrapper.in(ReviewOrder::getGovUnitId, Arrays.asList(reviewOrder.getGovUnitIdListStr().split(",")));
        }
        return wrapper;
    }

    @Override
    public Result<ReviewOrder> selectOne(ReviewOrder reviewOrder) {
        LambdaQueryWrapper<ReviewOrder> wrapper = this.commonWrapper(reviewOrder);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<ReviewOrder>> selectList(ReviewOrder reviewOrder) {
        LambdaQueryWrapper<ReviewOrder> wrapper = this.commonWrapper(reviewOrder);
        wrapper.orderByDesc(ReviewOrder::getCreateTime).orderByAsc(ReviewOrder::getId);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<ReviewOrder> selectPage(ReviewOrder reviewOrder) {
        LambdaQueryWrapper<ReviewOrder> wrapper = this.commonWrapper(reviewOrder);
        wrapper.orderByDesc(ReviewOrder::getCreateTime).orderByAsc(ReviewOrder::getId);
        return super.selectPage(reviewOrder, wrapper);
    }

    @Override
    public PageInfo<ReviewOrder> selectUPage(ReviewOrder reviewOrder) {
        PageInfo<ReviewOrder> reviewOrderPageInfo = this.selectPage(reviewOrder);
        List<ReviewOrder> list = reviewOrderPageInfo.getList();
        Set<Long> govTypeList = new HashSet<>();
        list.forEach(reviewOrder1 -> {
            //获取剩余时间以及时间状态（剩余还是逾期）
            if (Objects.equals(reviewOrder1.getOrderStatus(), SysConstant.FINISH_ORDER_STATUS)) {
                int dayNum = SysUtils.getWorkDay(reviewOrder1.getReviewStartTime(), reviewOrder1.getReviewEndTime());
                reviewOrder1.setDayNum(dayNum);
                //获取逾期时间
                Date expirationDate = reviewOrder1.getExpirationDate();
                if (expirationDate.getTime() - reviewOrder1.getReviewEndTime().getTime() < 0){
                    reviewOrder1.setDayType(-1);
                }else{
                    reviewOrder1.setDayType(1);
                }
            }else if(Objects.equals(reviewOrder1.getOrderStatus(), SysConstant.ING_ORDER_STATUS)){
                //获取逾期时间
                Date expirationDate = SysUtils.getDate(reviewOrder1.getExpirationDate());
                Date nowDate = new Date();
                int dayNum = 0;
                if (expirationDate.getTime() - nowDate.getTime() < 0) {
                    dayNum = SysUtils.getWorkDay(expirationDate,nowDate);
                    reviewOrder1.setDayType(-1);
                } else {
                    dayNum = SysUtils.getWorkDay(nowDate,expirationDate);
                    reviewOrder1.setDayType(1);
                }
                reviewOrder1.setDayNum(dayNum);
            }
            govTypeList.add(reviewOrder1.getGovType());
        });
        if (!CollectionUtils.isEmpty(govTypeList)) {
            Result<List<Gov>> listResult = govService.selectList(new Gov().setIdList(govTypeList));
            if (listResult.isSuccess()) {
                List<Gov> govList = listResult.getData();
                Map<Long, Gov> map = new HashMap<>();
                govList.forEach(gov -> {
                    map.put(gov.getId(), gov);
                });
                list.forEach(reviewOrder1 -> {
                    reviewOrder1.setGov(map.get(reviewOrder1.getGovType()));
                });
            }
        }
        return reviewOrderPageInfo;
    }

    @Override
    public Result<List<ReviewOrderExcel>> selectListByExcel(ReviewOrder reviewOrder) {
        PageInfo<ReviewOrder> reviewOrderPageInfo = this.selectPage(reviewOrder);
        List<ReviewOrder> list = reviewOrderPageInfo.getList();
        if (!CollectionUtils.isEmpty(list)) {
            List<ReviewOrderExcel> listExcel = new ArrayList<>();
            list.forEach(reviewOrder1 -> {
                ReviewOrderExcel reviewOrderExcel = new ReviewOrderExcel();
                BeanUtils.copyProperties(reviewOrder1, reviewOrderExcel);
                listExcel.add(reviewOrderExcel);
            });

            return new Result<List<ReviewOrderExcel>>().setSuccess(true).setData(listExcel);
        }
        return new Result<List<ReviewOrderExcel>>().setSuccess(false).setCode(SysConstant.SELECT_FAIL_CODE_RES);
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        ReviewOrder reviewOrder = new ReviewOrder().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(reviewOrder);
    }

}